<template>
  <div>
    <!-- 添加或修改申请使用对话框 -->
    <el-dialog title="查看-使用申请" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="68px">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="使用对象" prop="useTarget"
              ><el-select
                v-model="form.useTarget"
                placeholder="批量选择使用对象"
                @change="useTargetChange"
              >
                <el-option
                  v-for="item in ['部门', '个人']"
                  :key="item"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="存放地点" prop="place">
              <el-input
                v-model="form.place"
                placeholder="批量输入存放地点"
                @change="placeChange"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table :data="form.item" height="330">
              <el-table-column fixed type="index" label="序号">
              </el-table-column>
              <!-- <el-table-column prop="code" label="固定资产编号" align="center">
              </el-table-column> -->
              <el-table-column prop="name" label="名称" align="center">
              </el-table-column>
              <el-table-column prop="assetTypeName" label="类别" align="center">
              </el-table-column>
              <el-table-column prop="model" label="规格型号" align="center">
              </el-table-column>
              <el-table-column prop="model" label="使用对象" align="center">
                <template slot-scope="scope">
                  <el-select
                    v-model="form.item[scope.$index].useTarget"
                    placeholder="请选择使用对象"
                  >
                    <el-option
                      v-for="item in ['部门', '个人']"
                      :key="item"
                      :label="item"
                      :value="item"
                    >
                    </el-option>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column prop="model" label="存放地点" align="center">
                <template slot-scope="scope">
                  <el-input
                    v-model="form.item[scope.$index].place"
                    placeholder="请输入存放地点"
                  />
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row class="mt20">
          <el-col :span="24">
            <el-form-item label="申请原因" prop="remark">
              <el-input
                type="textarea"
                v-model="form.remark"
                placeholder="请输入购买理由"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="() => (this.open = false)">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageList as assetList } from "@/api/prod/asset/account";
import { add, get, edit } from "@/api/prod/use/apply";

export default {
  name: "AddApply",
  data() {
    const baseForm = {
      item: [
        {
          assetId: null,
          place: null,
          useTarget: null,
        },
      ],
      place: null,
      remarks: null,
      useTarget: null,
    };

    return {
      // 表单类型
      type: null,
      // 是否显示弹出层
      open: false,
      // 基础表单参数
      baseForm,
      // 表单参数
      form: { ...baseForm },
      // 表单校验
      rules: {},
    };
  },

  computed: {
    // 弹出层标题
    title() {
      if (this.type !== null) return this.type + "-使用申请";
      return "";
    },
  },

  methods: {
    // 表单重置
    reset() {
      this.type = null;
      this.form = { ...this.baseForm };
    },

    /** 弹出框打开 */
    show(type, pdata, id) {
      this.reset();
      this.open = true;
      this.type = type;

      if (pdata != null && pdata.multipleSelection != null) {
        assetList({ ids: pdata.multipleSelection }).then((response) => {
          this.form.item = response.data.map((item) => {
            item.assetId = item.id;
            item.id = null;
            return item;
          });
        });
      }

      if (id != null && id != undefined) {
        get(id).then((response) => {
          this.form = response.data;
        });
      }
    },

    /** 总使用对象改变事件 */
    useTargetChange() {
      this.form.item.forEach((item) =>
        this.$set(item, "useTarget", this.form.useTarget)
      );
    },

    /** 总地点改变事件 */
    placeChange() {
      console.log("???");
      this.form.item.forEach((item) =>
        this.$set(item, "place", this.form.place)
      );
      console.log(this.form.item);
    },

    /** 提交按钮 */
    submitForm(action) {
      this.form.action = action;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.type == "修改") {
            edit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("refresh");
            });
          } else {
            add(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("refresh");
            });
          }
        }
      });
    },
  },
};
</script>

<style>
</style>